package com.ktjiaoyu.crm.controller;

import com.ktjiaoyu.crm.pojo.User;
import com.ktjiaoyu.crm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 去到登录页面
     * @return
     */
    @RequestMapping(value = "/toLogin")
    public String toLogin(){

        return "login";
    }

    /**
     * 登录
     * @param usrName
     * @param usrPassword
     * @param model
     * @param session
     * @return
     */
    @RequestMapping(value = "/login")
    public String login(String usrName, String usrPassword, Model model, HttpSession session)throws Exception{
           User user = userService.login(usrName,usrPassword);
           if(user!=null){
               session.setAttribute("loginUser",user);
               return "main";
           }
           model.addAttribute("msg","用户名或密码错误，登录失败！");

           return "forward:/toLogin";
    }

    /**
     * 注销
     * @param session
     * @return
     */
    @RequestMapping(value = "/logout")
    public String logout(HttpSession session){
        session.removeAttribute("loginUser");   //删除session中保存的用户信息
        return "login";
    }


}
